// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="DirectorySystemInfoExtensions.cs" company="Microsoft.Blueprints.Contrib Team">
//   (C) 2009 Microsoft.Blueprints.Contrib Team
// </copyright>
// <summary>
//   Defines the DirectorySystemInfoExtensions type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------

namespace System.IO
{
    #region Using Directives

    using System.Collections.Generic;
    using System.Linq;

    #endregion

    /// <summary>
    /// A collection of Extension Methods for the DirectorySystemInfo type.
    /// </summary>
    public static class DirectorySystemInfoExtensions
    {
        /// <summary>
        /// Given a directory which contains collection of subdirectories
        /// that match WiX release version number, this extension method
        /// will return the name of the folder containing the latest.
        /// release.
        /// </summary>
        /// <param name="directoryInfo">
        /// The parent directory containing all the child Wix directories.
        /// </param>
        /// <returns>
        /// The name of the highest WiX version.
        /// </returns>
        public static string LatestWiXRelease(this DirectoryInfo directoryInfo)
        {
            string[] subDirectories = Directory.GetDirectories(directoryInfo.FullName);

            List<DirectoryInfo> directories = new List<DirectoryInfo>();

            foreach (string directory in subDirectories)
            {
                directories.Add(new DirectoryInfo(directory));
            }

            return directories.OrderByDescending(directory => directory.Name).Take(1).First().Name;
        }
    }
}